﻿/****************************************************************************
** file: CameraSettingDialog.h
** brief: 海康相机的支持代码，用来设置相机的工作参数。
** Copyright (C) LiYuan
** Author: LiYuan
** E-Mail: 18069211#qq(.)com
** Version 1.0.1
** Last modified: 2019.02.02
** Modified By: LiYuan
** TODO: 需要进一步修改，使得这个对话框支持各种相机。
** TODO: Auto ROI 设置的代码还没有完成。需要补充。
** TODO: 这个代码完善后应该移动到 CameraCommon 文件夹，成为通用的相机参数设置对话框。
****************************************************************************/

#include "CameraToupCamSettingDialog.h"
#include "ui_CameraToupCamSettingDialog.h"
#include <QDebug>
#include "CameraToupCam.h"

namespace Qly
{
CameraToupCamSettingDialog::CameraToupCamSettingDialog(CameraToupCam *camera, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::CameraToupCamSettingDialog),
    m_camera(camera)
{
    ui->setupUi(this);
    setModal(true);
    setAttribute(Qt::WA_DeleteOnClose, false);

    initExposureTab();
    initColorTab();
    initImageTab();
    initROITab();

    enableBrightnessUI(false);
    enableContrastUI(false);
    enableBlackLevelUI(false);
    enableGainUI(false);

    enableExposureTimeUI(false);
    enableAutoExposureUI(false);
    enableGammaUI(false);
    enableHueUI(false);
    enableSaturationUI(false);
    enableHueUI(false);
    enableSaturationUI(false);
    enableSharpnessUI(false);
    enableDenoiseUI(false);
    enableAutoROIUI(false);

    connect(ui->pushButtonWhiteBalanceOnePush, SIGNAL(clicked(bool)), this, SLOT(onWhiteBalanceOnePush()));
    connect(ui->pushButtonExposureOnePush, SIGNAL(clicked(bool)), this, SLOT(onExposureOnePush()));
    ui->pushButtonExposureOnePush->setEnabled(false);

    connect(ui->checkBoxAutoExposure, SIGNAL(toggled(bool)), this, SLOT(changeAutoExposure(bool)));
    connect(ui->checkBoxAutoWhiteBalance, SIGNAL(toggled(bool)), this, SLOT(changeAutoWhiteBalance(bool)));

    connect(ui->horizontalSliderBrightness, SIGNAL(valueChanged(int)), this, SLOT(changeBrightness(int)));
    connect(ui->horizontalSliderContrast, SIGNAL(valueChanged(int)), this, SLOT(changeContrast(int)));
    connect(ui->horizontalSliderBlacklevel, SIGNAL(valueChanged(int)), this, SLOT(changeBlackLevel(int)));
    connect(ui->horizontalSliderGain, SIGNAL(valueChanged(int)), this, SLOT(changeGain(int)));

    connect(ui->horizontalSliderExposureTime, SIGNAL(valueChanged(int)), this, SLOT(changeExposureTime(int)));

    connect(ui->horizontalSliderHue, SIGNAL(valueChanged(int)), this, SLOT(changeHue(int)));
    connect(ui->horizontalSliderSaturation, SIGNAL(valueChanged(int)), this, SLOT(changeSaturation(int)));

    connect(ui->horizontalSliderTemp, SIGNAL(valueChanged(int)), this, SLOT(changeWhiteBalanceTemp(int)));
    connect(ui->horizontalSliderTint, SIGNAL(valueChanged(int)), this, SLOT(changeWhiteBalanceTint(int)));

    connect(ui->horizontalSliderSharpness, SIGNAL(valueChanged(int)), this, SLOT(changeSharpness(int)));
    connect(ui->horizontalSliderGamma, SIGNAL(valueChanged(int)), this, SLOT(changeGamma(int)));
    connect(ui->horizontalSliderDenoise, SIGNAL(valueChanged(int)), this, SLOT(changeDenoise(int)));

    connect(ui->checkBoxHue, SIGNAL(toggled(bool)), this, SLOT(changeHueEnable(bool)));
    connect(ui->checkBoxSaturation, SIGNAL(toggled(bool)), this, SLOT(changeSaturationEnable(bool)));
    connect(ui->checkBoxSharpness, SIGNAL(toggled(bool)), this, SLOT(changeSharpnessEnable(bool)));
    connect(ui->checkBoxGamma, SIGNAL(toggled(bool)), this, SLOT(changeGammaEnable(bool)));
    connect(ui->checkBoxDenoise, SIGNAL(toggled(bool)), this, SLOT(changeDenoiseEnable(bool)));

    connect(ui->comboBoxResolution, QOverload<int>::of(&QComboBox::currentIndexChanged),
            this, &CameraToupCamSettingDialog::changeResolution);
}

void CameraToupCamSettingDialog::updateAutoExposureValue()
{
    m_camera->setExposureTimeMode(IndustryCamera::OFF);
    double value;
    m_camera->exposureTime(value);
    setExposureTime(value);
}

void CameraToupCamSettingDialog::initRoiUI()
{
    qDebug() << "updateROIInfo()";
    if(m_camera)
    {
        QStringList list = m_camera->resolution();
        qDebug() << "m_camera->resolution()";
        ui->comboBoxResolution->clear();
        qDebug() << "m_camera->clear()";
        ui->comboBoxResolution->addItems(list);
        qDebug() << "m_camera->addItems()";
        QSize size, min, max;
        m_camera->imageSize(size, max, min);

        setROIRange(0, max.width() - 2,
                    0, max.height() - 2,
                    2, max.width(),
                    2, max.height());
    }

}

void CameraToupCamSettingDialog::changeResolution(int N)
{
//    if(!m_update) return;
    if(N < 0) return;
    if(ui->comboBoxResolution->count() == 0) return;
    qDebug() << "changeResolution(int N)" << N;
    if(!m_camera) return;
    m_camera->setResolutionByIdx(N);
//    initRoiUI();

}

void CameraToupCamSettingDialog::changeDenoise(int value)
{
    if(!m_update) return;
    CameraToupCamImageProcessing * p = (CameraToupCamImageProcessing *) m_camera->imageProcessing();
    p->setDenoise(value);
}

void CameraToupCamSettingDialog::changeWhiteBalanceTemp(int value)
{
//    if(!m_update) return;
    qDebug() << "setWhiteBalanceRed = " << value;
    int tint = ui->spinBoxTint->value();
    CameraToupCamImageProcessing * p = (CameraToupCamImageProcessing *) m_camera->imageProcessing();
    p->setWhiteBalanceTempTint(value, tint);
}

void CameraToupCamSettingDialog::changeWhiteBalanceTint(int value)
{
//    if(!m_update) return;
    int temp = ui->spinBoxTemp->value();
    CameraToupCamImageProcessing * p = (CameraToupCamImageProcessing *) m_camera->imageProcessing();
    p->setWhiteBalanceTempTint(temp, value);
}

void CameraToupCamSettingDialog::changeHue(int value)
{
    if(!m_update) return;
    CameraToupCamImageProcessing * p = (CameraToupCamImageProcessing *) m_camera->imageProcessing();
    p->setHue(value);
}

void CameraToupCamSettingDialog::changeSaturation(int value)
{
    if(!m_update) return;
    CameraToupCamImageProcessing * p = (CameraToupCamImageProcessing *) m_camera->imageProcessing();
    p->setSaturation(value);
}

void CameraToupCamSettingDialog::changeAutoGain(bool on)
{
    if(!m_update) return;
    CameraToupCamImageProcessing * p = (CameraToupCamImageProcessing *) m_camera->imageProcessing();
    p->setAutoGain(on);
}

void CameraToupCamSettingDialog::changeAutoExposure(bool on)
{
    if(!m_update) return;

    m_camera->setAutoExpoureTime(on);
    if(on)
    {
        enableExposureTimeUI(false);
    }
    else
    {
//        double value;
//        m_update = false;
//        m_camera->expoureTime(value);
//        setExposureTime(value);
        enableExposureTimeUI(true);
//        m_update = true;
    }
}

void CameraToupCamSettingDialog::changeAutoWhiteBalance(bool on)
{
    m_camera->setAutoWhiteBalance(on);
    if(on)
    {  // 开启自动白平衡后就不能获取白平衡数据了
        ui->horizontalSliderTemp->setEnabled(false);
        ui->horizontalSliderTint->setEnabled(false);

        ui->spinBoxTemp->setEnabled(false);
        ui->spinBoxTint->setEnabled(false);

        ui->pushButtonWhiteBalanceOnePush->setEnabled(false);
    }
    else
    {
        m_update = false;
        int nMin, nMax, nValue;
        if(on = m_camera->whiteBalanceTempRange(nMin, nMax, nValue))
        {
            ui->spinBoxTemp->setRange(nMin, nMax);
            ui->horizontalSliderTemp->setRange(nMin, nMax);
            ui->spinBoxTemp->setValue(nValue);
        }
        ui->spinBoxTemp->setEnabled(on);
        ui->horizontalSliderTemp->setEnabled(on);

        if(on = m_camera->whiteBalanceTintRange(nMin, nMax, nValue))
        {
            ui->spinBoxTint->setRange(nMin, nMax);
            ui->horizontalSliderTint->setRange(nMin, nMax);
            ui->spinBoxTint->setValue(nValue);
        }
        ui->spinBoxTint->setEnabled(on);
        ui->horizontalSliderTint->setEnabled(on);

        ui->pushButtonWhiteBalanceOnePush->setEnabled(true);
        m_update = true;
    }
}

void CameraToupCamSettingDialog::changeGain(int value)
{
    if(!m_update) return;
    CameraToupCamImageProcessing * p = (CameraToupCamImageProcessing *) m_camera->imageProcessing();
    p->setGain(value / 10.0);
}

void CameraToupCamSettingDialog::changeSharpness(int value)
{
    if(!m_update) return;
    CameraToupCamImageProcessing * p = (CameraToupCamImageProcessing *) m_camera->imageProcessing();
    p->setSharpness(value);
}

void CameraToupCamSettingDialog::changeBlackLevel(int value)
{
    if(!m_update) return;
    CameraToupCamImageProcessing * p = (CameraToupCamImageProcessing *) m_camera->imageProcessing();
    p->setBlackLevel(value);
}

void CameraToupCamSettingDialog::changeContrast(int value)
{
    if(!m_update) return;
    CameraToupCamImageProcessing * p = (CameraToupCamImageProcessing *) m_camera->imageProcessing();
    p->setContrast(value);
}

void CameraToupCamSettingDialog::changeGamma(int value)
{
    if(!m_update) return;
    double fValue = value / 10.0;
    CameraToupCamImageProcessing * p = (CameraToupCamImageProcessing *) m_camera->imageProcessing();
    p->setGamma(fValue);
}

void CameraToupCamSettingDialog::changeExposureTime(int value)
{
    if(!m_update) return;
    m_camera->setExpoureTime(value);
}

void CameraToupCamSettingDialog::changeBrightness(int value)
{
    if(!m_update) return;
    CameraToupCamImageProcessing * p = (CameraToupCamImageProcessing *) m_camera->imageProcessing();
    p->setBrightness(value);
}

void CameraToupCamSettingDialog::updateWhiteBalanceValue()
{
    int nMin, nMax, nValue;
    bool on;

    m_update = false;
    m_camera->setAutoWhiteBalance(false);
    if(on = m_camera->whiteBalanceTempRange(nMin, nMax, nValue))
    {
        ui->spinBoxTemp->setRange(nMin, nMax);
        ui->horizontalSliderTemp->setRange(nMin, nMax);
        ui->spinBoxTemp->setValue(nValue);
    }
    ui->spinBoxTemp->setEnabled(on);
    ui->horizontalSliderTemp->setEnabled(on);

    if(on = m_camera->whiteBalanceTintRange(nMin, nMax, nValue))
    {
        ui->spinBoxTint->setRange(nMin, nMax);
        ui->horizontalSliderTint->setRange(nMin, nMax);
        ui->spinBoxTint->setValue(nValue);
    }
    ui->spinBoxTint->setEnabled(on);
    ui->horizontalSliderTint->setEnabled(on);
}

void CameraToupCamSettingDialog::onWhiteBalanceOnePush()
{
    qDebug() << "onWhiteBalanceOnePush()";
    m_camera->setWhiteBalanceOnePush();
//    QTimer::singleShot(500, this, SLOT(updateWhiteBalanceValue()));
}

void CameraToupCamSettingDialog::onExposureOnePush()
{
    qDebug() << "onExposureOnePush()";
    m_camera->setExpoureTimeOnePush();
//    QTimer::singleShot(1000, this, SLOT(updateAutoExposureValue()));
}

void CameraToupCamSettingDialog::changeHueEnable(bool on)
{
    qDebug() << "changeHueEnable";
    CameraToupCamImageProcessing * p = (CameraToupCamImageProcessing *) m_camera->imageProcessing();
    if( p->setHueEnable(on) )
    {
        ui->horizontalSliderHue->setEnabled(on);
        ui->spinBoxHue->setEnabled(on);
    }
    if(!on) return;

    int min, max, value;
    if(p->hueRange(min, max, value))
    {
        setHueRange(min, max);
        setHue(value);
    }
}

void CameraToupCamSettingDialog::changeSaturationEnable(bool on)
{
    CameraToupCamImageProcessing * p = (CameraToupCamImageProcessing *) m_camera->imageProcessing();

    if( p->setSaturationEnable(on) )
    {
        ui->horizontalSliderSaturation->setEnabled(on);
        ui->spinBoxSaturation->setEnabled(on);
    }
    if(!on) return;

    int min, max, value;
    if(p->saturationRange(min, max, value))
    {
        setSaturationRange(min, max);
        setSaturation(value);
    }
}

void CameraToupCamSettingDialog::changeSharpnessEnable(bool on)
{
    if( p->setSharpnessEnable(on) )
    {
        ui->horizontalSliderSharpness->setEnabled(on);
        ui->spinBoxSharpness->setEnabled(on);
    }
    if(!on) return;

    int min, max, value;
    if(p->sharpnessRange(min, max, value))
    {
        setSharpnessRange(min, max);
        setSharpness(value);
    }
}

void CameraToupCamSettingDialog::changeGammaEnable(bool on)
{
    CameraToupCamImageProcessing * p = (CameraToupCamImageProcessing *) m_camera->imageProcessing();
    if( p->setGammaEnable(on) )
    {
        ui->horizontalSliderGamma->setEnabled(on);
        ui->spinBoxGamma->setEnabled(on);
    }
    if(!on) return;

    double min, max, value;
    if(p->gammaRange(min, max, value))
    {
        setGammaRange(min, max);
        setGamma(value);
    }
}

void CameraToupCamSettingDialog::changeDenoiseEnable(bool on)
{
    CameraToupCamImageProcessing * p = (CameraToupCamImageProcessing *) m_camera->imageProcessing();
    if( p->setDenoiseEnable(on) )
    {
        ui->horizontalSliderDenoise->setEnabled(on);
        ui->spinBoxDenoise->setEnabled(on);
    }
    if(!on) return;

    int min, max, value;
    if(p->denoiseRange(min, max, value))
    {
        setDenoiseRange(min, max);
        setDenoise(value);
    }
}

void CameraToupCamSettingDialog::initNodeInfo()
{   
    m_update = false;
    setWindowTitle(m_camera->cameraName());
    int nMin, nMax, nValue;
    bool on;
    CameraToupCamImageProcessing * p = (CameraToupCamImageProcessing *) m_camera->imageProcessing();

    if(on = p->brightnessRange(nMin, nMax, nValue))
    {
        setBrightnessRange(nMin, nMax);
        setBrightness(nValue);
    }
    enableBrightnessUI(on);
    //CameraToupCamImageProcessing * p = (CameraToupCamImageProcessing *) m_camera->imageProcessing();
    if(on = p->contrastRange(nMin, nMax, nValue))
    {
        setContrastRange(nMin, nMax);
        setContrast(nValue);
    }
    enableContrastUI(on);

    if(on = p->blackLevelRange(nMin, nMax, nValue))
    {
        setBlackLevelRange(nMin, nMax);
        setBlackLevel(nValue);
    }
    enableBlackLevelUI(on);

    double dMin, dMax, dValue;
    if(on = p->gainRange(dMin, dMax, dValue))
    {
        setGainRange(dMin, dMax);
        setGain(dValue);
    }
    enableGainUI(on);

    if(on = m_camera->exposureRange(dMin, dMax, dValue))
    {
        if(dMax > 1000000) dMax = 1000000;
        if(dMin < 10) dMin = 10;
        setExposureTimeRange(dMin, dMax);
        setExposureTime(dValue);
    }
    enableExposureTimeUI(on);

    if(on = p->blackLevelRange(nMin, nMax, nValue))
    {
        setBlackLevelRange(nMin, nMax);
        setBlackLevel(nValue);
    }
    enableBlackLevelUI(on);

    if(on = m_camera->autoExposureTimeLowerLimitRange(nMin, nMax, nValue))
    {
        setAutoExposureTimeLowerLimitRange(nMin, nMax);
        setAutoExposureTimeLowerLimit(nValue);
    }
    ui->spinBoxAutoExposureMin->setEnabled(on);

    if(on = m_camera->autoExposureTimeUpperLimitRange(nMin, nMax, nValue))
    {
        setAutoExposureTimeUpperLimitRange(nMin, nMax);
        setAutoExposureTimeUpperLimit(nValue);
    }
    ui->spinBoxAutoExposureMax->setEnabled(on);

    bool enable;
    ui->checkBoxAutoExposure->setChecked(false);
    if(on = m_camera->autoExposure(enable))
    {
        if(enable)
        ui->checkBoxAutoExposure->setChecked(true);
    }
    ui->checkBoxAutoExposure->setEnabled(on);
    ui->pushButtonExposureOnePush->setEnabled(on);

//----------------------------------------第二页-------------------------------------------

    initHueUI();
    initSaturationUI();
    initWhiteBalanceUI();

//--------------------------第三页----------------------------------

    initSharpnessUI();
    initDenoiseUI();
    initGammaUI();
    initFlipUI();

    //--------------------------第四页---------------------------
    if(ui->comboBoxResolution->count() == 0)
    {
        initRoiUI();
    }
    m_update = true;
}

void CameraToupCamSettingDialog::initFlipUI()
{
    bool on1, on2;
    bool ret = m_camera->getFlip(on1, on2);
    ui->checkBoxVFlip->setEnabled(ret);// 海康相机 SDK 现阶段不支持图像翻转
    ui->checkBoxHFlip->setEnabled(ret);
    ui->checkBoxVFlip->setChecked(on1);
    ui->checkBoxHFlip->setChecked(on2);

    connect(ui->checkBoxHFlip, SIGNAL(toggled(bool)), this, SLOT(changeHFlip(bool)));
    connect(ui->checkBoxVFlip, SIGNAL(toggled(bool)), this, SLOT(changeVFlip(bool)));

}

void CameraToupCamSettingDialog::changeVFlip(bool on)
{
    m_camera->setVFlip(on);
}

void CameraToupCamSettingDialog::changeHFlip(bool on)
{
    m_camera->setHFlip(on);
}

void CameraToupCamSettingDialog::setWhiteBalanceTempTint(int temp, int tint)
{
    ui->spinBoxTemp->setValue(temp);
    ui->spinBoxTint->setValue(tint);
}

void CameraToupCamSettingDialog::initWhiteBalanceUI()
{
    bool enable, on;
    int nMin, nMax, nValue;
    ui->checkBoxAutoWhiteBalance->setChecked(false);
    if(on = m_camera->autoWhiteBalance(enable))
    {
        qDebug() << "autoWhiteBalance is " << enable;
        ui->checkBoxAutoWhiteBalance->setChecked(enable);
    }
    ui->checkBoxAutoWhiteBalance->setEnabled(on);
    ui->pushButtonWhiteBalanceOnePush->setEnabled(on);

    if(on = m_camera->whiteBalanceTempRange(nMin, nMax, nValue))
    {
        ui->spinBoxTemp->setRange(nMin, nMax);
        ui->horizontalSliderTemp->setRange(nMin, nMax);
        ui->spinBoxTemp->setValue(nValue);
    }
    ui->spinBoxTemp->setEnabled(on);
    ui->horizontalSliderTemp->setEnabled(on);

    if(on = m_camera->whiteBalanceTintRange(nMin, nMax, nValue))
    {
        ui->spinBoxTint->setRange(nMin, nMax);
        ui->horizontalSliderTint->setRange(nMin, nMax);
        ui->spinBoxTint->setValue(nValue);
    }
    ui->spinBoxTint->setEnabled(on);
    ui->horizontalSliderTint->setEnabled(on);
}

void CameraToupCamSettingDialog::initSharpnessUI()
{
    bool bValue;
    int nMin, nMax, nValue;
    bool ret1 = m_camera->sharpnessEnable(bValue);
    bool ret2 = m_camera->sharpnessRange(nMin, nMax, nValue);

    if(ret1 && ret2)
    {
        ui->checkBoxSharpness->setEnabled(true);
        ui->checkBoxSharpness->setChecked(bValue);
        setSharpnessRange(nMin, nMax);
        setSharpness(nValue);
        enableSharpnessUI(true);
        return;
    }
    ui->checkBoxSharpness->setEnabled(false);
    enableSharpnessUI(false);
}

void CameraToupCamSettingDialog::initGammaUI()
{
    bool bValue;
    double fMin, fMax, fValue;
    bool ret1 = m_camera->gammaEnable(bValue);
    bool ret2 = m_camera->gammaRange(fMin, fMax, fValue);

    if(ret1 && ret2)
    {
        ui->checkBoxGamma->setEnabled(true);
        ui->checkBoxGamma->setChecked(bValue);
        setGammaRange(fMin, fMax);
        setGamma(fValue);
        enableGammaUI(true);
        return;
    }
    ui->checkBoxGamma->setEnabled(false);
    enableGammaUI(false);
}

void CameraToupCamSettingDialog::initDenoiseUI()
{
    bool bValue;
    int nMin, nMax, nValue;
    bool ret1 = m_camera->denoiseEnable(bValue);
    bool ret2 = m_camera->denoiseRange(nMin, nMax, nValue);

    if(ret1 && ret2)
    {
        ui->checkBoxDenoise->setEnabled(true);
        ui->checkBoxDenoise->setChecked(bValue);
        setDenoiseRange(nMin, nMax);
        setDenoise(nValue);
        enableDenoiseUI(true);
        return;
    }
    ui->checkBoxDenoise->setEnabled(false);
    enableDenoiseUI(false);
}

void CameraToupCamSettingDialog::initSaturationUI()
{
    bool bValue;
    int nMin, nMax, nValue;
    bool ret1 = m_camera->saturationEnable(bValue);
    bool ret2 = m_camera->saturationRange(nMin, nMax, nValue);

    if(ret1 && ret2)
    {
        ui->checkBoxSaturation->setEnabled(true);
        ui->checkBoxSaturation->setChecked(bValue);
        setSaturationRange(nMin, nMax);
        setSaturation(nValue);
        enableSaturationUI(true);
        return;
    }
    ui->checkBoxSaturation->setEnabled(false);
    enableSaturationUI(false);
}

void CameraToupCamSettingDialog::initHueUI()
{
    bool bValue;
    int nMin, nMax, nValue;
    bool ret1 = m_camera->hueEnable(bValue);
    bool ret2 = m_camera->hueRange(nMin, nMax, nValue);

    if(ret1 && ret2)
    {
        ui->checkBoxHue->setEnabled(true);
        ui->checkBoxHue->setChecked(bValue);
        setHueRange(nMin, nMax);
        setHue(nValue);
        enableHueUI(true);
        return;
    }
    ui->checkBoxHue->setEnabled(false);
    enableHueUI(false);
}

void CameraToupCamSettingDialog::setAutoExposureTimeLowerLimitRange(int min, int max)
{
    ui->spinBoxAutoExposureMin->setRange(min, max);
}

void CameraToupCamSettingDialog::setAutoExposureTimeLowerLimit(int value)
{
    ui->spinBoxAutoExposureMin->setValue(value);
}

void CameraToupCamSettingDialog::setAutoExposureTimeUpperLimitRange(int min, int max)
{
    ui->spinBoxAutoExposureMax->setRange(min, max);
}

void CameraToupCamSettingDialog::setAutoExposureTimeUpperLimit(int value)
{
    ui->spinBoxAutoExposureMax->setValue(value);
}

void CameraToupCamSettingDialog::setContrastRange(int min, int max)
{
    ui->horizontalSliderContrast->setRange(min, max);
    ui->spinBoxContrast->setRange(min, max);
}

void CameraToupCamSettingDialog::setContrast(int value)
{
    ui->horizontalSliderContrast->setValue(value);
}

void CameraToupCamSettingDialog::enableBrightnessUI(bool on)
{
    ui->spinBoxBrightness->setEnabled(on);
    ui->horizontalSliderBrightness->setEnabled(on);
}

void CameraToupCamSettingDialog::enableContrastUI(bool on)
{
    ui->spinBoxContrast->setEnabled(on);
    ui->horizontalSliderContrast->setEnabled(on);
}

void CameraToupCamSettingDialog::enableBlackLevelUI(bool on)
{
    ui->spinBoxBlacklevel->setEnabled(on);
    ui->horizontalSliderBlacklevel->setEnabled(on);
}

void CameraToupCamSettingDialog::enableGainUI(bool on)
{
    ui->spinBoxGain->setEnabled(on);
    ui->horizontalSliderGain->setEnabled(on);
}

void CameraToupCamSettingDialog::enableExposureTimeUI(bool on)
{
    ui->spinBoxExposureTime->setEnabled(on);
    ui->horizontalSliderExposureTime->setEnabled(on);
}

void CameraToupCamSettingDialog::enableAutoExposureUI(bool on)
{
    ui->spinBoxAutoExposureMin->setEnabled(on);
    ui->spinBoxAutoExposureMax->setEnabled(on);
}

void CameraToupCamSettingDialog::enableGammaUI(bool on)
{
    ui->spinBoxGamma->setEnabled(on);
    ui->horizontalSliderGamma->setEnabled(on);
}

void CameraToupCamSettingDialog::enableHueUI(bool on)
{
    ui->spinBoxHue->setEnabled(on);
    ui->horizontalSliderHue->setEnabled(on);
}

void CameraToupCamSettingDialog::enableSaturationUI(bool on)
{
    ui->spinBoxSaturation->setEnabled(on);
    ui->horizontalSliderSaturation->setEnabled(on);
}

void CameraToupCamSettingDialog::enableSharpnessUI(bool on)
{
    ui->spinBoxSharpness->setEnabled(on);
    ui->horizontalSliderSharpness->setEnabled(on);
}

void CameraToupCamSettingDialog::enableDenoiseUI(bool on)
{
    ui->spinBoxDenoise->setEnabled(on);
    ui->horizontalSliderDenoise->setEnabled(on);
}

void CameraToupCamSettingDialog::enableAutoROIUI(bool on)
{

}

void CameraToupCamSettingDialog::setROI(QRect roi)
{
    ui->spinBoxLeft->setValue(roi.left());
    ui->spinBoxTop->setValue(roi.top());
    ui->spinBoxWidth->setValue(roi.width());
    ui->spinBoxHeight->setValue(roi.height());
}

void CameraToupCamSettingDialog::setROIRange(int xOffsetMin, int xOffsetMax, int yOffsetMin, int yOffsetMax, int xWidthMin, int xWidthMax, int yHeightMin, int yHeightMax)
{
    ui->spinBoxLeft->setRange(xOffsetMin, xOffsetMax);
    ui->horizontalSliderLeft->setRange(xOffsetMin, xOffsetMax);
    ui->spinBoxTop->setRange(yOffsetMin, yOffsetMax);
    ui->horizontalSliderTop->setRange(yOffsetMin, yOffsetMax);
    ui->spinBoxWidth->setRange(xWidthMin, xWidthMax);
    ui->horizontalSliderWidth->setRange(xWidthMin, xWidthMax);
    ui->spinBoxHeight->setRange(yHeightMin, yHeightMax);
    ui->horizontalSliderHeight->setRange(yHeightMin, yHeightMax);
}

void CameraToupCamSettingDialog::setExposureTimeRange(int min, int max)
{
    //qDebug() << "begin setExposureTimeRange";
    ui->horizontalSliderExposureTime->setRange(min, max);
    ui->spinBoxExposureTime->setRange(min, max);
    //qDebug() << "end setExposureTimeRange";
}

void CameraToupCamSettingDialog::setExposureTime(int value)
{
    ui->spinBoxExposureTime->setValue(value);
}

void CameraToupCamSettingDialog::setGammaRange(double min, double max)
{
    ui->horizontalSliderGamma->setRange(min * 10, max * 10);
    ui->spinBoxGamma->setRange(min * 10, max * 10);
}

void CameraToupCamSettingDialog::setGamma(double value)
{
    ui->horizontalSliderGamma->setValue(value * 10);
}

void CameraToupCamSettingDialog::setBrightnessRange(int min, int max)
{
    ui->horizontalSliderBrightness->setRange(min, max);
    ui->spinBoxBrightness->setRange(min, max);
}

void CameraToupCamSettingDialog::setBrightness(int value)
{
    ui->horizontalSliderBrightness->setValue(value);
}

void CameraToupCamSettingDialog::setGainRange(double min, double max)
{
    ui->horizontalSliderGain->setRange(min * 10, max * 10);
    ui->spinBoxGain->setRange(min * 10, max * 10);
}

void CameraToupCamSettingDialog::setGain(double value)
{
    ui->horizontalSliderGain->setValue(value * 10);
}

void CameraToupCamSettingDialog::setBlackLevelRange(int min, int max)
{
    ui->spinBoxBlacklevel->setRange(min, max);
    ui->horizontalSliderBlacklevel->setRange(min, max);
}

void CameraToupCamSettingDialog::setBlackLevel(int value)
{
    ui->spinBoxBlacklevel->setValue(value);
}

void CameraToupCamSettingDialog::setHueRange(int min, int max)
{
    ui->spinBoxHue->setRange(min, max);
    ui->horizontalSliderHue->setRange(min, max);
}

void CameraToupCamSettingDialog::setHue(int value)
{
    ui->spinBoxHue->setValue(value);
}

void CameraToupCamSettingDialog::setSaturationRange(int min, int max)
{
    ui->spinBoxSaturation->setRange(min, max);
    ui->horizontalSliderSaturation->setRange(min, max);
}

void CameraToupCamSettingDialog::setSaturation(int value)
{
    ui->spinBoxSaturation->setValue(value);
}

void CameraToupCamSettingDialog::setSharpnessRange(int min, int max)
{
    ui->spinBoxSharpness->setRange(min, max);
    ui->horizontalSliderSharpness->setRange(min, max);
}

void CameraToupCamSettingDialog::setSharpness(int value)
{
    ui->spinBoxSharpness->setValue(value);
}

void CameraToupCamSettingDialog::setDenoiseRange(int min, int max)
{
    ui->spinBoxDenoise->setRange(min, max);
    ui->horizontalSliderDenoise->setRange(min, max);
}

void CameraToupCamSettingDialog::setDenoise(int value)
{
    ui->spinBoxDenoise->setValue(value);
}


void CameraToupCamSettingDialog::initExposureTab()
{
    connect(ui->spinBoxBrightness,SIGNAL(valueChanged(int)), ui->horizontalSliderBrightness, SLOT(setValue(int)));
    connect(ui->horizontalSliderBrightness, SIGNAL(valueChanged(int)), ui->spinBoxBrightness, SLOT(setValue(int)));

    connect(ui->spinBoxContrast,SIGNAL(valueChanged(int)), ui->horizontalSliderContrast, SLOT(setValue(int)));
    connect(ui->horizontalSliderContrast, SIGNAL(valueChanged(int)), ui->spinBoxContrast, SLOT(setValue(int)));

    connect(ui->spinBoxBlacklevel,SIGNAL(valueChanged(int)), ui->horizontalSliderBlacklevel, SLOT(setValue(int)));
    connect(ui->horizontalSliderBlacklevel, SIGNAL(valueChanged(int)), ui->spinBoxBlacklevel, SLOT(setValue(int)));

    connect(ui->spinBoxExposureTime,SIGNAL(valueChanged(int)), ui->horizontalSliderExposureTime, SLOT(setValue(int)));
    connect(ui->horizontalSliderExposureTime, SIGNAL(valueChanged(int)), ui->spinBoxExposureTime, SLOT(setValue(int)));

    connect(ui->spinBoxGain,SIGNAL(valueChanged(int)), ui->horizontalSliderGain, SLOT(setValue(int)));
    connect(ui->horizontalSliderGain, SIGNAL(valueChanged(int)), ui->spinBoxGain, SLOT(setValue(int)));

}

void CameraToupCamSettingDialog::initImageTab()
{
    connect(ui->spinBoxSharpness,SIGNAL(valueChanged(int)), ui->horizontalSliderSharpness, SLOT(setValue(int)));
    connect(ui->horizontalSliderSharpness, SIGNAL(valueChanged(int)), ui->spinBoxSharpness, SLOT(setValue(int)));

    connect(ui->spinBoxGamma,SIGNAL(valueChanged(int)), ui->horizontalSliderGamma, SLOT(setValue(int)));
    connect(ui->horizontalSliderGamma, SIGNAL(valueChanged(int)), ui->spinBoxGamma, SLOT(setValue(int)));

    connect(ui->spinBoxDenoise,SIGNAL(valueChanged(int)), ui->horizontalSliderDenoise, SLOT(setValue(int)));
    connect(ui->horizontalSliderDenoise, SIGNAL(valueChanged(int)), ui->spinBoxDenoise, SLOT(setValue(int)));
}

void CameraToupCamSettingDialog::initROITab()
{
    connect(ui->spinBoxLeft,SIGNAL(valueChanged(int)), ui->horizontalSliderLeft, SLOT(setValue(int)));
    connect(ui->horizontalSliderLeft, SIGNAL(valueChanged(int)), ui->spinBoxLeft, SLOT(setValue(int)));

    connect(ui->spinBoxTop,SIGNAL(valueChanged(int)), ui->horizontalSliderTop, SLOT(setValue(int)));
    connect(ui->horizontalSliderTop, SIGNAL(valueChanged(int)), ui->spinBoxTop, SLOT(setValue(int)));

    connect(ui->spinBoxWidth,SIGNAL(valueChanged(int)), ui->horizontalSliderWidth, SLOT(setValue(int)));
    connect(ui->horizontalSliderWidth, SIGNAL(valueChanged(int)), ui->spinBoxWidth, SLOT(setValue(int)));

    connect(ui->spinBoxHeight,SIGNAL(valueChanged(int)), ui->horizontalSliderHeight, SLOT(setValue(int)));
    connect(ui->horizontalSliderHeight, SIGNAL(valueChanged(int)), ui->spinBoxHeight, SLOT(setValue(int)));
}

void CameraToupCamSettingDialog::initColorTab()
{
    connect(ui->spinBoxHue,SIGNAL(valueChanged(int)), ui->horizontalSliderHue, SLOT(setValue(int)));
    connect(ui->horizontalSliderHue, SIGNAL(valueChanged(int)), ui->spinBoxHue, SLOT(setValue(int)));

    connect(ui->spinBoxSaturation,SIGNAL(valueChanged(int)), ui->horizontalSliderSaturation, SLOT(setValue(int)));
    connect(ui->horizontalSliderSaturation, SIGNAL(valueChanged(int)), ui->spinBoxSaturation, SLOT(setValue(int)));

    connect(ui->spinBoxTemp,SIGNAL(valueChanged(int)), ui->horizontalSliderTemp, SLOT(setValue(int)));
    connect(ui->horizontalSliderTemp, SIGNAL(valueChanged(int)), ui->spinBoxTemp, SLOT(setValue(int)));

    connect(ui->spinBoxTint,SIGNAL(valueChanged(int)), ui->horizontalSliderTint, SLOT(setValue(int)));
    connect(ui->horizontalSliderTint, SIGNAL(valueChanged(int)), ui->spinBoxTint, SLOT(setValue(int)));
}

CameraToupCamSettingDialog::~CameraToupCamSettingDialog()
{
    delete ui;
}
} //namespace Qly
